
surface.CreateFont( "Type-Ra", 18, 400, true, false, "ScoreboardText" );


Scoreboard = nil;

function AddScoreboardRow( ply )

	table.insert( Scoreboard.PlayerList, ply );

	local x, y = Scoreboard:GetPos();
	local w, h = Scoreboard:GetSize();
	
	y = y - 40;
	h = h + 40;
	
	Scoreboard:SetPos( x, y );
	Scoreboard:SetSize( w, h );
	
	local c = 1;
	
	for k, v in pairs( Scoreboard.PlayerList ) do
	
		c = c + 1;
		Scoreboard.PlayersAdded[ply:UserID()] = k;
	
	end
	
	local img = vgui.Create( "AvatarImage", Scoreboard );
	img:SetPlayer( ply );
	img:SetPos( 10, 99 + ( #Scoreboard.PlayerList - 1 ) * 40 );
	img:SetSize( 32, 32 );
	
	Scoreboard:AddObject( img );
	
	Scoreboard.Icons[ply:UserID()] = img;

end

function RemoveScoreboardRow( key, key2 )

	Scoreboard.PlayersAdded[key2] = nil;
	Scoreboard.PlayerList[key] = nil;
	
	if( Scoreboard.Icons[key] ) then
	
		Scoreboard.Icons[key]:Remove();
		Scoreboard.Icons[key] = nil;
		
	end
	
	local x, y = Scoreboard:GetPos();
	local w, h = Scoreboard:GetSize();
	
	y = y + 40;
	h = h - 40;
	
	Scoreboard:SetPos( x, y );
	Scoreboard:SetSize( w, h );
	
	local tlist = { }
	local c = 1;
	
	for k, v in pairs( Scoreboard.PlayerList ) do
	
		if( v:IsValid() ) then
	
			local id = v:UserID();
			
			if( Scoreboard.Icons[id] ) then
			
				Scoreboard.Icons[id]:SetPos( 10, 99 + ( c - 1 ) * 40 );
				tlist[c] = v;
				
				c = c + 1;
				
			end
				
		end
	
	end	
	
	Scoreboard.PlayerList = tlist;

end

function DrawScoreboardRows()

	local sy = 105;
	
	for k, v in pairs( Scoreboard.PlayerList ) do
	
		if( v:IsValid() ) then
	
			draw.RoundedBox( 0, 8, 97 + ( k - 1 ) * 40, 36, 36, Color( 255, 255, 255, 255 ) );
		
			local nick = v:Nick();
			
			if( string.len( nick ) > 20 ) then
			
				nick = string.sub( nick, 1, 20 ) .. "..";
			
			end
		
			draw.DrawText( nick, "ScoreboardText", 50, sy + ( k - 1 ) * 40, Color( 255, 255, 255, 255 ) );
			draw.DrawText( v:Ping() .. "   MS", "ScoreboardText", 490, sy + ( k - 1 ) * 40, Color( 255, 255, 255, 255 ), 2 );
		
		end
		
	end

end

function GM:CreateScoreboard()
 
 	Scoreboard = CreateBPanel( nil, ScrW() * .5 - 250, ScrH() * .5 - 50, 500, 100 );
 	Scoreboard.BodyColor = Color( 0, 0, 0, 230 );
 	Scoreboard.PlayerList = { }
 	Scoreboard.PlayersAdded = { }
 	Scoreboard.Icons = { }
 
 	Scoreboard:AddLabel( "Epidemic.", "OpeningEpidemic", 10, 5, Color( 255, 255, 255, 255 ) );
 
 	Scoreboard.PaintHook = function()
 	
 		draw.DrawText( string.gsub( "Server: " .. GetHostName(), " ", "   " ), "OpeningEpidemicLinks", 10, 70, Color( 255, 255, 255, 255 ) );
 		draw.RoundedBox( 0, 10, 90, 480, 2, Color( 255, 255, 255, 255 ) ); 
 	
 		DrawScoreboardRows();
 	
 	end
 	
 	Scoreboard.Think = function()
 	
 		local list = { }
 	
 		for k, v in pairs( player.GetAll() ) do
 		
 			if( not Scoreboard.PlayersAdded[v:UserID()] ) then
 			
 				list[v:UserID()] = { };
 				AddScoreboardRow( v );
 				
 			else
 			
 				list[v:UserID()] = { };
 			
 			end
 		
 		end
 		
 		for k, v in pairs( Scoreboard.PlayersAdded ) do
 		
 			if( not list[k] ) then
 			
 				RemoveScoreboardRow( v, k );
 			
 			end
 		
 		end
 	
 	end
 
end


function GM:ScoreboardShow()

	if( not Scoreboard ) then
	
		self:CreateScoreboard();
	
	end

	Scoreboard:SetVisible( true );

end


function GM:ScoreboardHide()
 
	Scoreboard:SetVisible( false );
 
end